package org.okeoe.gen.ctl;


import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.okeoe.gen.entity.CgTemplateEntity;
import org.okeoe.gen.entity.dto.CgTemplateDTO;
import org.okeoe.gen.services.CgTemplateAccessServices;
import org.okeoe.gen.services.CgTemplateConfigServices;
import org.okeoe.gen.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author dj
 * @version V1.0
 * @Title: 模板 mybatis dao
 * @date 2019-10-28 18:04:47
 */
@Slf4j
@RestController
@RequestMapping("/cgTemplateCtl")
@Api(tags = "模板")
public class CgTemplateCtl {
    @Autowired
    private CgTemplateAccessServices cgTemplateAccessServicesImpl;
    @Autowired
    private CgTemplateConfigServices cgTemplateConfigServicesImpl;

    /**
     * 分页查询
     *
     * @param pageNum  起始页
     * @param pageSize 大小
     * @return 分页数据
     * @throws Exception
     */
    @GetMapping("/pageList")
    @ApiOperation(value = "模板" + BaseMsg.MODULAR_PAGE, notes = "模板" + BaseMsg.MODULAR_PAGE)
    public PageInfo<CgTemplateDTO> pageCgTemplateList(Integer pageNum, Integer pageSize, CgTemplateDTO dto, HttpServletRequest request) throws Exception {
        return cgTemplateAccessServicesImpl.pageList(pageNum, pageSize, dto);

    }

    /**
     * 保存
     *
     * @param entityName
     * @return 成功失败
     * @throws Exception
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "模板" + BaseMsg.MODULAR_ADD, notes = "模板" + BaseMsg.MODULAR_ADD)
    public ResultMsg addCgTemplate(@Validated({AddView.class}) CgTemplateEntity entityName, HttpServletRequest request) throws Exception {

        return cgTemplateConfigServicesImpl.insertSelective(entityName);
    }

    /**
     * 更新
     *
     * @param updateEntityDto
     * @return 成功失败
     * @throws Exception
     */
    @PostMapping(value = "/update")
    @ApiOperation(value = "模板" + BaseMsg.MODULAR_UPDATE, notes = "模板" + BaseMsg.MODULAR_ADD)
    public ResultMsg updateCgTemplate(@Validated({UpdateView.class}) CgTemplateEntity updateEntityDto, HttpServletRequest request) throws Exception {
        CgTemplateEntity entity = cgTemplateAccessServicesImpl.selectById(updateEntityDto.getId());
        ObjectCopyUtils.copyPropertiesIf(updateEntityDto, entity, BaseConstant.UPDATE);

        return cgTemplateConfigServicesImpl.updateSelectiveById(entity);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return 查询对象
     * @throws Exception
     */
    @GetMapping(value = "/findById")
    @ApiOperation(value = "模板" + BaseMsg.MODULAR_FIND_ID, notes = "模板" + BaseMsg.MODULAR_FIND_ID)
    public ResultMsg findCgTemplateById(String id) throws Exception {
        CgTemplateEntity entityName = cgTemplateAccessServicesImpl.selectById(id);
        return ResultMsg.success(entityName);
    }

    /**
     * 删除
     *
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
    @PostMapping(value = "/delById")
    @ApiOperation(value = "模板" + BaseMsg.MODULAR_DEL, notes = "模板" + BaseMsg.MODULAR_DEL)
    public ResultMsg delCgTemplateById(String ids) throws Exception {
        for (String id : ids.split(BaseConstant.SPOT)) {
            if (StringUtils.isBlank(id)) {
                continue;
            }
            CgTemplateEntity entityName = cgTemplateAccessServicesImpl.selectById(id);
            if (null != entityName && entityName.getIsEnable()) {
                throw new MyException("使用的模板不能删除！");
            }
        }
        return cgTemplateConfigServicesImpl.deleteByIds(ids.split(BaseConstant.SPOT));
    }

}